<template>
  <div class="file-list">
    <div class="file-header">
      <div class="header-title">文件列表</div>
    </div>
    
    <el-scrollbar class="file-scrollbar">
      <div class="file-items">
        <div 
          v-for="file in fileList" 
          :key="file.id" 
          class="file-item"
          :class="{ 'is-active': file.id === currentFileId }"
          @click="handleSelectFile(file)"
        >
          <div class="file-name">
            <i class="el-icon-document"></i>
            <span>{{ file.name }}</span>
          </div>
          <div class="file-status">
            <el-tag v-if="file.signStatus === 1" size="small" type="success">已设置</el-tag>
          <el-tag v-else size="small" type="info">未设置</el-tag>
          </div>
        </div>
      </div>
    </el-scrollbar>
  </div>
</template>

<script>
import { ref, defineComponent } from 'vue'

export default defineComponent({
  name: 'FileList',
  
  props: {
    // 文件列表
    fileList: {
      type: Array,
      default: () => []
    },
    // 当前选中的文件ID
    currentFileId: {
      type: String,
      default: ''
    }
  },
  
  emits: ['select-file'],
  
  setup(props, { emit }) {
    /**
     * 处理选择文件
     * @param {Object} file - 文件信息
     */
    const handleSelectFile = (file) => {
      emit('select-file', file)
    }
    
    /**
     * 更新文件签章状态
     * @param {Object} statusMap - 文件ID与签章状态的映射
     */
    const updateFileSignStatus = (statusMap) => {
      if (!statusMap || typeof statusMap !== 'object') return
      
      props.fileList.forEach(file => {
        if (statusMap[file.id] !== undefined) {
          file.signStatus = statusMap[file.id]
        }
      })
    }
    
    return {
      handleSelectFile,
      updateFileSignStatus
    }
  }
})
</script>

<style lang="scss" scoped>
.file-list {
  height: 100%;
  display: flex;
  flex-direction: column;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  
  .file-header {
    padding: 10px;
    border-bottom: 1px solid #dcdfe6;
    background-color: #f5f7fa;
    
    .header-title {
      font-weight: bold;
    }
  }
  
  .file-scrollbar {
    flex: 1;
    overflow: auto;
  }
  
  .file-items {
    .file-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px;
      border-bottom: 1px solid #ebeef5;
      cursor: pointer;
      
      &:hover {
        background-color: #f5f7fa;
      }
      
      &.is-active {
        background-color: #ecf5ff;
      }
      
      .file-name {
        display: flex;
        align-items: center;
        
        i {
          margin-right: 5px;
          color: #909399;
        }
        
        span {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
  }
}
</style>